home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-includes / streams.php < prev    next >
Encoding:
PHP Script  |  2005-04-20  |  3.4 KB  |  159 lines

  1. <?php
  2. /*
  3.    Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
  4.  
  5.    This file is part of PHP-gettext.
  6.  
  7.    PHP-gettext is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    PHP-gettext is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with PHP-gettext; if not, write to the Free Software
  19.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21. */
  22.  
  23.  
  24. // Simple class to wrap file streams, string streams, etc.
  25. // seek is essential, and it should be byte stream
  26. class StreamReader {
  27.   // should return a string [FIXME: perhaps return array of bytes?]
  28.   function read($bytes) {
  29.     return false;
  30.   }
  31.   
  32.   // should return new position
  33.   function seekto($position) {
  34.     return false;
  35.   }
  36.   
  37.   // returns current position
  38.   function currentpos() {
  39.     return false;
  40.   }
  41.   
  42.   // returns length of entire stream (limit for seekto()s)
  43.   function length() {
  44.     return false;
  45.   }
  46. }
  47.  
  48. class StringReader {
  49.   var $_pos;
  50.   var $_str;
  51.  
  52.   function StringReader($str='') {
  53.     $this->_str = $str;
  54.     $this->_pos = 0;
  55.   }
  56.  
  57.   function read($bytes) {
  58.     $data = substr($this->_str, $this->_pos, $bytes);
  59.     $this->_pos += $bytes;
  60.     if (strlen($this->_str)<$this->_pos)
  61.       $this->_pos = strlen($this->_str);
  62.  
  63.     return $data;
  64.   }
  65.  
  66.   function seekto($pos) {
  67.     $this->_pos = $pos;
  68.     if (strlen($this->_str)<$this->_pos)
  69.       $this->_pos = strlen($this->_str);
  70.     return $this->_pos;
  71.   }
  72.  
  73.   function currentpos() {
  74.     return $this->_pos;
  75.   }
  76.  
  77.   function length() {
  78.     return strlen($this->_str);
  79.   }
  80.  
  81. }
  82.  
  83.  
  84. class FileReader {
  85.   var $_pos;
  86.   var $_fd;
  87.   var $_length;
  88.  
  89.   function FileReader($filename) {
  90.     if (file_exists($filename)) {
  91.  
  92.       $this->_length=filesize($filename);
  93.       $this->_pos = 0;
  94.       $this->_fd = fopen($filename,'rb');
  95.       if (!$this->_fd) {
  96.     $this->error = 3; // Cannot read file, probably permissions
  97.     return false;
  98.       }
  99.     } else {
  100.       $this->error = 2; // File doesn't exist
  101.       return false;
  102.     }
  103.   }
  104.  
  105.   function read($bytes) {
  106.     if ($bytes) {
  107.       fseek($this->_fd, $this->_pos);
  108.       $data = fread($this->_fd, $bytes);
  109.       $this->_pos = ftell($this->_fd);
  110.       
  111.       return $data;
  112.     } else return '';
  113.   }
  114.  
  115.   function seekto($pos) {
  116.     fseek($this->_fd, $pos);
  117.     $this->_pos = ftell($this->_fd);
  118.     return $this->_pos;
  119.   }
  120.  
  121.   function currentpos() {
  122.     return $this->_pos;
  123.   }
  124.  
  125.   function length() {
  126.     return $this->_length;
  127.   }
  128.  
  129.   function close() {
  130.     fclose($this->_fd);
  131.   }
  132.  
  133. }
  134.  
  135. // Preloads entire file in memory first, then creates a StringReader 
  136. // over it (it assumes knowledge of StringReader internals)
  137. class CachedFileReader extends StringReader {
  138.   function CachedFileReader($filename) {
  139.     if (file_exists($filename)) {
  140.  
  141.       $length=filesize($filename);
  142.       $fd = fopen($filename,'rb');
  143.  
  144.       if (!$fd) {
  145.     $this->error = 3; // Cannot read file, probably permissions
  146.     return false;
  147.       }
  148.       $this->_str = fread($fd, $length);
  149.       fclose($fd);
  150.  
  151.     } else {
  152.       $this->error = 2; // File doesn't exist
  153.       return false;
  154.     }
  155.   }
  156. }
  157.  
  158.  
  159. ?>